// pages/detail/detail.js
Page({
    data: {
        gender: '女',
        username: 'zhangsan',
        imgUrl: "/images/avatar.jpeg",
        show: false
    },

    changeAvatar () {
      wx.chooseImage({
        count: 1,
        success: ({ tempFiles }) => {
          const avatarFile = tempFiles[0].path

          this.setData({
            imgUrl: avatarFile
          })

          const userInfo = wx.getStorageSync('userInfo') || {}
   
          wx.setStorageSync('userInfo', {
            ...userInfo,
            avatar: this.data.imgUrl
          })
        }
      })
    },
    
    onLoad() {
      const userInfo = wx.getStorageSync('userInfo')
      if (userInfo) {
        const { username,avatar,gender } = userInfo
        this.setData({
          gender,
          username,
          imgUrl: avatar
        })
      }
    },

    edit (e) {
       this.setData({
        show: true
       })
       
    },
    //完成信息修改
    formSubmit: function (e) {
      const { gender, username } = e.detail.value
      this.setData({
        gender,
        username,
        show: false,
       })

       const userInfo = wx.getStorageSync('userInfo')

       wx.setStorageSync('userInfo', {
         ...userInfo,
         gender,
         username
       })
       
    }
})